'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

import unittest, filecmp
from carcomp.framework import wrapper, component, abstraction
from carcomp.directory import localhost

class TestComp1(component.Implementation):
    
    def __init__(self, name):
        component.Implementation.__init__(self, name)
        self.add_port(component.Port('in', 'handler'))
        self.add_port(component.Port('out', '__OUT__'))
        
    def handler(self, msg):
        #print(msg)
        self.send_out('out', msg)

class TestAbs1(abstraction.Abstraction):
    
    def __init__(self, name):
        abstraction.Abstraction.__init__(self, name)
        comp = TestComp1('test_comp')
        self.add_component(comp)
        self.add_port('in', (comp, 'in'))
        self.add_port('out', (comp, 'out'), output=True)

input_file = '{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root)
output_file = '{}ft/tests/framework/TEST_OUTPUT.txt'.format(localhost.local_directory.package_root)

class Test(unittest.TestCase):

    def test_reader_count(self):
        wrp = wrapper.Wrapper()
        abs = TestAbs1('test_abs')
        in_tuple = ('in', input_file)
        out_tuple = ('out', output_file)
        wrp.add_abstraction(abs, [in_tuple], [out_tuple])
        self.assertEqual(len(wrp._reader_list), 1)
    
    def test_writer_count(self):
        wrp = wrapper.Wrapper()
        abs = TestAbs1('test_abs')
        in_tuple = ('in', input_file)
        out_tuple = ('out', output_file)
        wrp.add_abstraction(abs, [in_tuple], [out_tuple])
        self.assertEqual(len(wrp._writer_list), 1)
    
    def test_connector_count(self):
        wrp = wrapper.Wrapper()
        abs = TestAbs1('test_abs')
        in_tuple = ('in', input_file)
        out_tuple = ('out', output_file)
        wrp.add_abstraction(abs, [in_tuple], [out_tuple])
        self.assertEqual(len(wrp._conn_list), 2)
    
    def test_component_count(self):
        wrp = wrapper.Wrapper()
        abs = TestAbs1('test_abs')
        in_tuple = ('in', input_file)
        out_tuple = ('out', output_file)
        wrp.add_abstraction(abs, [in_tuple], [out_tuple])
        self.assertEqual(len(wrp._comp_list), 3)
        
    def test_run(self):
        wrp = wrapper.Wrapper()
        abs = TestAbs1('test_abs')
        in_tuple = ('in', input_file)
        out_tuple = ('out', output_file)
        wrp.add_abstraction(abs, [in_tuple], [out_tuple])
        wrp.run()
        test_out_file = output_file + '_TEST.txt'
        ret = filecmp.cmp(output_file, test_out_file, False)
        self.assertEqual(ret, True)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_']
    unittest.main()